package com.bootdo.lgs.ordertemp.service.impl;

import com.bootdo.lgs.ordertemp.dao.OrderTempMapper;
import com.bootdo.lgs.ordertemp.model.OrderTemp;
import com.bootdo.lgs.ordertemp.service.OrderTempService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class OrderTempServiceImpl implements OrderTempService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderTempServiceImpl.class);

    @Autowired
    OrderTempMapper orderTempMapper;

    @Override
    public List<OrderTemp> getList(Map<String, Object> params) {
        return orderTempMapper.getList(params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(Map<String, Object> params) {
        params.put("record_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return orderTempMapper.add(params) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(Map<String, Object> params) {
        params.put("update_date", new Date());
        return orderTempMapper.update(params) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchUpdate(List<Map<String, Object>> conditions) {
        Date now = new Date();
        return orderTempMapper.batchUpdate(conditions.stream().peek(item -> item.put("update_date", now)).collect(Collectors.toList())) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(Long id) {
        return orderTempMapper.delete(id) > 0;
    }
}
